const sendMessage = (instance) => {
  let text = instance.input.get();
  let channel = instance.data.channel;

  if (text || text != '') {
    let options = {
      channelId: channel._id,
      details: { text: text }
    };
    Meteor.call('sendChannelMessage', options, (err, res) => {
      if (res) {
        instance.input.set('');
        $('#channel-chatroom-input').val('');
      }
    });
  }
};

Template.channel_chatroom_footer.onCreated(function () {
  this.input = new ReactiveVar();
});

Template.channel_chatroom_footer.events({
  'input #channel-chatroom-input' (e, instance) {
    instance.input.set($.trim(e.target.value));
  },

  'keypress #channel-chatroom-input' (e, instance) {
    if (e.keyCode === 13) {
      sendMessage(instance);
    }
  },

  'click #send-channel-message' (e, instance) {
    sendMessage(instance);
  }
});